package com.lz.rabbitmq.direct;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConsumerDirect {
    /**
     * 注册交换机
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("directExchange", true, false);
    }

    /**
     * 注册队列
     */
    @Bean
    public Queue directQueue() {
        return new Queue("directQueue", true, false, false);
    }

    /**
     * 绑定交换机和队列
     */
    @Bean
    public Binding bind() {
        return BindingBuilder.bind(directQueue()).to(directExchange()).with("direct");
    }

    /**
     * 消费消息
     */
    @RabbitListener(queues = "directQueue")
    public void process(String msg) {
        System.out.println("directQueue接收到消息：" + msg);
    }
}
